// 1.创建服务器
const fs = require('fs')
const path = require('path')
const http = require('http')

const server = http.createServer()

server.on('request',(req,res)=>{
    // 2.获取客户端输入的url
    const url = req.url
    // 3.拼接路径，获取本地资源
    const fpath = path.join(__dirname,url)
    // 4.读取本地资源
    fs.readFile(fpath,'utf8',(err,data)=>{
        if(err){
            return res.end('<h1>404 Not Found</h1>')
        }
        // 5.响应返回给客户端
        res.end(data)
    })
})

server.listen('81',()=>{
    console.log('running at http://127.0.0.1:81')
})